package class02;

import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeMap;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class HashMapAndSortedMap {
    public static void main(String[] args) {
//      ===============  HashMap  ===============
//      ===============无序  O(1)  自定义类作为key时，必须重新比较器===============
        HashMap<Integer,String> map = new HashMap<>();
        map.put(1,"我是1");
        map.put(2,"我是2");
        map.put(3,"我是3");
        map.put(4,"我是4");
        map.put(5,"我是5");
        map.put(6,"我是6");
        map.put(10000,"我是7");

        System.out.println(map.containsKey(1));
        System.out.println(map.containsValue("我是7"));
        System.out.println(map.get(4));
        System.out.println(map.get(10));
        map.put(4,"他是4");
        System.out.println(map.get(4));
        map.remove(4);
        System.out.println(map.get(4));

//      ===============  TreeMap  ==============
//      ===============  有序  ===============
        TreeMap<Integer,String> treeMap = new TreeMap<>();
        treeMap.put(3, "我是3");
        treeMap.put(4, "我是4");
        treeMap.put(8, "我是8");
        treeMap.put(5, "我是5");
        treeMap.put(7, "我是7");
        treeMap.put(1, "我是1");
        treeMap.put(2, "我是2");

        System.out.println(treeMap.containsKey(1));
        System.out.println(treeMap.containsKey(10));
        System.out.println(treeMap.get(4));
        System.out.println(treeMap.get(10));
        treeMap.put(4,"他是4");
        System.out.println(treeMap.get(4));
        treeMap.remove(4);
        System.out.println(treeMap.get(4));

        System.out.println(treeMap.firstKey());
        System.out.println(treeMap.lastKey());
        //<=4
        System.out.println(treeMap.floorKey(4));
        //>=4
        System.out.println(treeMap.ceilingKey(4));

//      ===============  HashSet  ===============
        HashSet<String> set = new HashSet<>();
        set.add("abc");
        set.contains("abc");
        set.remove("abc");
    }
}
